
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

import javax.swing.JLayeredPane;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.ScrollPane;

import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.ListSelectionModel;


public class ViewLogsUI extends JPanel {
	private JTable table;
	private String tableName;
	private String assetName;
	private String dateTime;
	private String assetID;
	private int selectedAsset;
	
	private Asset myAsset;
	private LogMethods log;
	
	private JLabel txtName;
	private JLabel txtOwner;
	private JLabel txtCustodian;
	private JLabel txtType;
	private JLabel txtMaintenanceSchedule;
	private JLabel txtDateAcquired;
	private JLabel txtRetentionPeriod;
	private JLabel txtFinancial;
	private JLabel txtConfidentiality;
	private JLabel txtIntegrity;
	private JLabel txtAvailability;
	private JLabel txtStorageLocation;
	private JLabel txtClassification;
	private JComboBox<String> comboAssetName;
	private ArrayList<String> assetNames;
	JScrollPane scrollPane;
	private JLabel lblSelectAsset;

	/**
	 * Create the panel.
	 */
	
	public ViewLogsUI() {
		setLayout(null);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(20, 43, 621, 165);

		add(scrollPane);
		
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent event) {
				 int row = table.rowAtPoint(event.getPoint());
				 int col = table.columnAtPoint(event.getPoint());
				 
				 tableName = table.getModel().getValueAt(row, 4).toString();
				 assetName = table.getModel().getValueAt(row, 2).toString();
				 assetID = table.getModel().getValueAt(row, 1).toString();
				 dateTime = table.getModel().getValueAt(row, 0).toString();
	 
				 if (tableName.compareTo("Add")== 0){
					 tableName = "addlog";
					 setValues();}
				 else if (tableName.compareTo("Edit")==0){
					 tableName = "editlog";
					 setValues();}
				 else if (tableName.compareTo("Delete")==0){
					 tableName = "deletelog";
					 setValuesForDelete();}
			}
		});
		table.setEnabled(false);
		scrollPane.setViewportView(table);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null},
			},
			new String[] {
				"DateTime", "Type of Action", "Asset ID", "Asset name", "Username"
			}
		));
		table.getColumnModel().getColumn(1).setPreferredWidth(92);
		table.getColumnModel().getColumn(2).setPreferredWidth(80);
		table.setRowSelectionAllowed(true);
		
		log = new LogMethods();
		log.ViewLog(table);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblName.setBounds(55, 216, 52, 14);
		add(lblName);
		
		JLabel lblOwner = new JLabel("Owner:");
		lblOwner.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblOwner.setBounds(55, 269, 52, 14);
		add(lblOwner);
		
		JLabel lblCustodian = new JLabel("Custodian:");
		lblCustodian.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblCustodian.setBounds(55, 324, 72, 14);
		add(lblCustodian);
		
		JLabel lblType = new JLabel("Type:");
		lblType.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblType.setBounds(306, 216, 52, 14);
		add(lblType);
		
		JLabel lblDateAcquired = new JLabel("Date Acquired:");
		lblDateAcquired.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblDateAcquired.setBounds(55, 437, 105, 14);
		add(lblDateAcquired);
		
		JLabel lblRetentionPeriod = new JLabel("Retention Period:");
		lblRetentionPeriod.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblRetentionPeriod.setBounds(55, 492, 148, 14);
		add(lblRetentionPeriod);
		
		JLabel lblMaintenanceSchedule = new JLabel("Maintenance Schedule:");
		lblMaintenanceSchedule.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblMaintenanceSchedule.setBounds(306, 247, 148, 14);
		add(lblMaintenanceSchedule);
		
		JLabel lblValue = new JLabel("Value:");
		lblValue.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblValue.setBounds(388, 324, 55, 24);
		add(lblValue);
		
		JLabel lblFinancial = new JLabel("Financial:");
		lblFinancial.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblFinancial.setBounds(304, 360, 73, 14);
		add(lblFinancial);
		
		JLabel lblConfidentiality = new JLabel("Confidentiality:");
		lblConfidentiality.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblConfidentiality.setBounds(304, 390, 110, 14);
		add(lblConfidentiality);
		
		JLabel lblIntegrity = new JLabel("Integrity:");
		lblIntegrity.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblIntegrity.setBounds(304, 420, 73, 14);
		add(lblIntegrity);
		
		JLabel lblAvailability = new JLabel("Availability:");
		lblAvailability.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblAvailability.setBounds(304, 450, 73, 14);
		add(lblAvailability);
		
		JLabel lblClassification = new JLabel("Classification:");
		lblClassification.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblClassification.setBounds(306, 278, 93, 14);
		add(lblClassification);
		
		JLabel lblStorageLocation = new JLabel("Storage Location:");
		lblStorageLocation.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblStorageLocation.setBounds(55, 379, 125, 14);
		add(lblStorageLocation);
		
		txtName = new JLabel();
		txtName.setBounds(55, 238, 195, 26);
		add(txtName);
		
		txtOwner = new JLabel();
		txtOwner.setBounds(55, 293, 200, 25);
		add(txtOwner);
		
		txtCustodian = new JLabel();
		txtCustodian.setBounds(55, 348, 200, 25);
		add(txtCustodian);

		txtType = new JLabel();
		txtType.setBounds(461, 216, 100, 20);
		add(txtType);
		
		txtMaintenanceSchedule = new JLabel();
		txtMaintenanceSchedule.setBounds(461, 247, 100, 20);
		add(txtMaintenanceSchedule);
		
		txtDateAcquired = new JLabel();
		txtDateAcquired.setBounds(55, 461, 195, 20);
		add(txtDateAcquired);
		
		txtRetentionPeriod = new JLabel();
		txtRetentionPeriod.setBounds(55, 516, 200, 20);
		add(txtRetentionPeriod);
		
		txtConfidentiality = new JLabel();
		txtConfidentiality.setBounds(461, 390, 100, 20);
		add(txtConfidentiality);
		
		txtIntegrity = new JLabel();
		txtIntegrity.setBounds(461, 417, 100, 20);
		add(txtIntegrity);
		
		txtAvailability = new JLabel();
		txtAvailability.setBounds(461, 447, 100, 20);
		add(txtAvailability);
		
		txtClassification = new JLabel();
		txtClassification.setBounds(461, 278, 100, 20);
		add(txtClassification);
	
		assetNames = new ArrayList<String>();
		
		comboAssetName = new JComboBox<String>();
		comboAssetName.addItem("All");
		setcomboBox();
		comboAssetName.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (comboAssetName.getSelectedItem()!= null && comboAssetName.getSelectedItem().toString().compareTo("All") != 0){
					selectedAsset = getSelectedID();
					log.ViewLogByID(table, selectedAsset);
				}
				else
					log.ViewLog(table);
			}
		});
		comboAssetName.setBounds(126, 14, 195, 20);
		add(comboAssetName);		
		
		txtStorageLocation = new JLabel();;
		txtStorageLocation.setBounds(55, 403, 200, 25);
		add(txtStorageLocation);
		
		txtFinancial = new JLabel();
		txtFinancial.setBounds(461, 360, 72, 15);
		add(txtFinancial);
		
		lblSelectAsset = new JLabel("Select Asset:");
		lblSelectAsset.setFont(new Font("SansSerif", Font.BOLD, 14));
		lblSelectAsset.setBounds(20, 15, 107, 16);
		add(lblSelectAsset);
	}
	
	private int getSelectedID()
	{
		StringTokenizer tokenizer = new StringTokenizer(comboAssetName.getSelectedItem().toString(), "-");
		tokenizer.nextToken(); //go past the name
		return Integer.parseInt(tokenizer.nextToken());
	}
	
	public void setcomboBox(){
		comboAssetName.removeAllItems();
		comboAssetName.addItem("All");
		assetNames.removeAll(assetNames);
		String currString;
		int rowCount = table.getRowCount() - 1;	
		
		while (rowCount >= 0){
			currString = table.getModel().getValueAt(rowCount, 2).toString()+"-"+table.getModel().getValueAt(rowCount, 1).toString();
			if(!assetNames.contains(currString)) {
				assetNames.add(currString);
				rowCount--;
			}
			else
				rowCount--;
		}
		
		Collections.sort(assetNames);
		
		for(int i = 0; i < assetNames.size(); i++)
			comboAssetName.addItem(assetNames.get(i));
		}
	 
	private void setValues(){
		myAsset = Asset.createAssetFromIDTableDateTime(Integer.parseInt(assetID), tableName, dateTime);
		txtName.setText(myAsset.getName());
		txtOwner.setText(myAsset.getOwner());
		txtCustodian.setText(myAsset.getCustodian());
		txtType.setText(myAsset.getTypeDesc());
		txtMaintenanceSchedule.setText(myAsset.getMaintenanceScheduleDesc());
		txtDateAcquired.setText(myAsset.getDateAcquired().toString());
		txtRetentionPeriod.setText(myAsset.getRetentionPeriod().toString());
		txtFinancial.setText(myAsset.getFinancialDesc());
		txtConfidentiality.setText(myAsset.getConfidentialityDesc());
		txtIntegrity.setText(myAsset.getIntegrityDesc());
		txtAvailability.setText(myAsset.getAvailabilityDesc());
		txtClassification.setText(myAsset.getClassificationDesc());
		txtStorageLocation.setText(myAsset.getStorageLocation());
	}
	
	private void setValuesForDelete(){
		//myAsset = Asset.createAssetFromName(assetName);
		myAsset = Asset.createAssetFromID(Integer.parseInt(assetID));
		txtName.setText(myAsset.getName());
		txtOwner.setText(myAsset.getOwner());
		txtCustodian.setText(myAsset.getCustodian());
		txtType.setText(myAsset.getTypeDesc());
		txtMaintenanceSchedule.setText(myAsset.getMaintenanceScheduleDesc());
		txtDateAcquired.setText(myAsset.getDateAcquired().toString());
		txtRetentionPeriod.setText(myAsset.getRetentionPeriod().toString());
		txtFinancial.setText(myAsset.getFinancialDesc());
		txtConfidentiality.setText(myAsset.getConfidentialityDesc());
		txtIntegrity.setText(myAsset.getIntegrityDesc());
		txtAvailability.setText(myAsset.getAvailabilityDesc());
		txtClassification.setText(myAsset.getClassificationDesc());
		txtStorageLocation.setText(myAsset.getStorageLocation());
	}
}
